package com.shwread.android.qysw.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciko.code.bean.MyBusinessTripBean;
import com.mywork.R;
import com.shwread.android.qysw.activity.MyBusinessTripDetailActivity;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 因公外出申请列表adapter
 */
public class BusinessTripAdapter extends BaseAdapter {

    private List<MyBusinessTripBean> list;
    private Context context;
    private int type;//类型，，为1时是我的外勤，为2时是收到的外勤
    private final int SEND = 1;//发出的签到
    private final int RECEIVE = 2;//收到的签到

    public BusinessTripAdapter(Context context, List<MyBusinessTripBean> list, int type) {
        this.context = context;
        this.list = list;
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder mh = null;
        if (view == null) {
            mh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_business_trip, null);
            mh.businessTrip= (LinearLayout) view.findViewById(R.id.businessTrip);
            mh.ll_item_myfieldwork_reason = (RelativeLayout) view.findViewById(R.id.ll_item_myfieldwork_reason);
            mh.tv_item_myfieldwork_date = (TextView) view.findViewById(R.id.tv_item_myfieldwork_date);
            mh.tv_item_myfieldwork_reason = (TextView) view.findViewById(R.id.tv_item_myfieldwork_reason);
            mh.tv_item_myfieldwork_name = (TextView) view.findViewById(R.id.tv_item_myfieldwork_name);
            mh.tv_item_myfieldwork_end_date = (TextView) view.findViewById(R.id.tv_item_myfieldwork_end_date);
            mh.tv_item_myfieldwork_status = (TextView) view.findViewById(R.id.tv_item_myfieldwork_status);
            mh.text_zhi= (TextView) view.findViewById(R.id.text_zhi);
            view.setTag(mh);
        } else {
            mh = (ViewHolder) view.getTag();
        }
        final MyBusinessTripBean myBusinessTripBean = list.get(i);
        mh.tv_item_myfieldwork_reason.setText("拜访" + myBusinessTripBean.getCustName());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Long time = new Long(myBusinessTripBean.getBeginVisitTime());
        Long time1 = new Long(myBusinessTripBean.getEndVisitTime());
        String beginVisitTime = format.format(time);
        String endVisitTime = format.format(time1);
        mh.tv_item_myfieldwork_date.setText(beginVisitTime);
        mh.tv_item_myfieldwork_end_date.setText(endVisitTime);

        if (myBusinessTripBean.getStatus() == 0) {
            mh.tv_item_myfieldwork_status.setText("待审批");
        } else if (myBusinessTripBean.getStatus() == 1) {
            mh.tv_item_myfieldwork_status.setText(Html.fromHtml(
                    "<font color='#ff0000'>" + "待签到" + "</font>"));
            mh.tv_item_myfieldwork_reason.setText(Html.fromHtml(
                    "<font color='#ff0000'>" + "拜访" + myBusinessTripBean.getCustName()+ "</font>"));
            mh.tv_item_myfieldwork_date.setText(Html.fromHtml(
                    "<font color='#ff0000'>" +beginVisitTime+ "</font>"));
            mh.tv_item_myfieldwork_end_date.setText(Html.fromHtml(
                    "<font color='#ff0000'>" +endVisitTime+ "</font>"));
            mh.text_zhi.setText(Html.fromHtml(
                    "<font color='#ff0000'>" +"至"+ "</font>"));
            mh.tv_item_myfieldwork_name.setText(Html.fromHtml(
                    "<font color='#ff0000'>" +myBusinessTripBean.getUserName()+ "</font>"));
        } else if (myBusinessTripBean.getStatus() == 2) {
            mh.tv_item_myfieldwork_status.setText("待反馈");

        } else if (myBusinessTripBean.getStatus() == 3) {
            mh.tv_item_myfieldwork_status.setText("已反馈");

        } else if (myBusinessTripBean.getStatus() == 4) {
            mh.tv_item_myfieldwork_status.setText("审批不通过");

        }


        if (type == SEND) {
            mh.tv_item_myfieldwork_name.setVisibility(View.GONE);
        } else if (type == RECEIVE) {
            mh.tv_item_myfieldwork_name.setVisibility(View.VISIBLE);
            if (myBusinessTripBean.getStatus() == 0) {
                mh.tv_item_myfieldwork_status.setText(Html.fromHtml(
                        "<font color='#ff0000'>" + "待审批" + "</font>"));
                mh.tv_item_myfieldwork_reason.setText(Html.fromHtml(
                        "<font color='#ff0000'>" + "拜访" + myBusinessTripBean.getCustName()+ "</font>"));
                mh.tv_item_myfieldwork_date.setText(Html.fromHtml(
                        "<font color='#ff0000'>" +beginVisitTime+ "</font>"));
                mh.tv_item_myfieldwork_end_date.setText(Html.fromHtml(
                        "<font color='#ff0000'>" +endVisitTime+ "</font>"));
                mh.tv_item_myfieldwork_name.setText(Html.fromHtml(
                        "<font color='#ff0000'>" +myBusinessTripBean.getUserName()+ "</font>"));
                mh.text_zhi.setText(Html.fromHtml(
                        "<font color='#ff0000'>" +"至"+ "</font>"));
            } else if (myBusinessTripBean.getStatus() == 1) {
                mh.tv_item_myfieldwork_status.setText(Html.fromHtml(
                        "<font color='#000000'>" + "待签到" + "</font>"));
                mh.tv_item_myfieldwork_reason.setText(Html.fromHtml(
                        "<font color='#000000'>" + "拜访" + myBusinessTripBean.getCustName()+ "</font>"));
                mh.tv_item_myfieldwork_date.setText(Html.fromHtml(
                        "<font color='#000000'>" +beginVisitTime+ "</font>"));
                mh.tv_item_myfieldwork_end_date.setText(Html.fromHtml(
                        "<font color='#000000'>" +endVisitTime+ "</font>"));
                mh.tv_item_myfieldwork_name.setText(Html.fromHtml(
                        "<font color='#000000'>" +myBusinessTripBean.getUserName()+ "</font>"));
                mh.text_zhi.setText(Html.fromHtml(
                        "<font color='#000000'>" +"至"+ "</font>"));
            }else {
                mh.tv_item_myfieldwork_reason.setText(Html.fromHtml(
                        "<font color='#000000'>" + "拜访" + myBusinessTripBean.getCustName()+ "</font>"));
                mh.tv_item_myfieldwork_date.setText(Html.fromHtml(
                        "<font color='#000000'>" +beginVisitTime+ "</font>"));
                mh.tv_item_myfieldwork_end_date.setText(Html.fromHtml(
                        "<font color='#000000'>" +endVisitTime+ "</font>"));
                mh.tv_item_myfieldwork_name.setText(Html.fromHtml(
                        "<font color='#000000'>" +myBusinessTripBean.getUserName()+ "</font>"));
                mh.text_zhi.setText(Html.fromHtml(
                        "<font color='#000000'>" +"至"+ "</font>"));

            }

        }
        mh.businessTrip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MyBusinessTripDetailActivity.class);
                intent.putExtra("type", type);
                Log.e("type", String.valueOf(type));
                intent.putExtra("bean", myBusinessTripBean);
                context.startActivity(intent);
            }
        });
        return view;
    }

    private class ViewHolder {
        private LinearLayout businessTrip;
        private RelativeLayout ll_item_myfieldwork_reason;

        private TextView tv_item_myfieldwork_date,text_zhi,
                tv_item_myfieldwork_reason, tv_item_myfieldwork_name, tv_item_myfieldwork_end_date, tv_item_myfieldwork_status;
    }


}
